class Solution:
    def isValid(self, s: str) -> bool:
        if len(s) & 1 == 1:
            return False
        pairs = {'(': ')', '[': ']', '{': '}'}
        stack = list()
        for ch in s:
            if ch in pairs:
                    if not stack or stack[-1] != pairs[ch]:
                        return False
            else:
                stack.append(ch)
        return not stack
